package com.zjitc.controller.admin;

import com.github.pagehelper.PageInfo;
import com.zjitc.model.Category;
import com.zjitc.service.CategoryService;
import com.zjitc.service.OrderService;
import com.zjitc.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.List;

import static java.awt.SystemColor.info;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/10 0010
 * @description:
 */
@Controller
@RequestMapping(value = "/admin/category")
public class AdminCategroyControl {

    @Autowired
    private CategoryService service;

    @RequestMapping(value = "/findCategories")
    @ResponseBody
    public ModelAndView findOrders(
            @RequestParam(name = "pageNum", defaultValue = "1", required = false) int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "4", required = false) int pageSize) {
        ModelAndView mv = new ModelAndView(new InternalResourceView("/admin/category/list.jsp"));
        List<Category> categories = service.findCategories(pageNum, pageSize);
        PageInfo info = new PageInfo(categories);
        mv.addObject("page", info);
        return mv;
    }

    @RequestMapping(value = "/deleteByCid")
    @ResponseBody
    public String deleteByCid(String cid) {
        service.deleteByCid(cid);
        return cid;
    }

    @RequestMapping(value = "/findByCid")
    @ResponseBody
    public ModelAndView findByCid(String cid) {
        Category category = service.findByCid(cid);
        ModelAndView mv = new ModelAndView(new InternalResourceView("/admin/category/edit.jsp"));
        mv.addObject("category", category);
        return mv;
    }

    @RequestMapping(value = "/addCategory")
    @ResponseBody
    public void addCategory(String name) {
        String cid = Utils.getUUID();
        Category category = new Category();
        category.setCid(cid);
        category.setName(name);
        service.add(category);
    }

    @RequestMapping(value = "/updateCategory")
    public String updateCategory(Category category) {
        service.update(category);
        return "redirect:/admin/category/findCategories.do";
    }
}
